import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../view/Login.vue'
import Home from '../view/Home.vue'
import Welcome from '../view/Welcome.vue'
import Users from '../view/users/Users.vue'
import Rights from '../view/power/Rights.vue'
import Roles from '../view/power/Roles.vue'
import Cate from '../view/goods/Cate.vue'
import Params from '../view/goods/Params.vue'
import Goods from '../view/goods/Goods.vue'
import Add from '../view/goods/Add.vue'
import Orders from '../view/orders/Orders.vue'
import Reports from '../view/report/Reports.vue'
Vue.use(VueRouter)
    // 路由导航守卫 



const routes = [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
        redirect: '/welcome',
        path: '/home',
        component: Home,
        children: [
            { path: '/welcome', component: Welcome },
            { path: '/users', component: Users },
            { path: '/rights', component: Rights },
            { path: '/roles', component: Roles },
            { path: '/categories', component: Cate },
            { path: '/params', component: Params },
            { path: '/goods', component: Goods },
            { path: '/goods/add', component: Add },
            { path: '/orders', component: Orders },
            { path: '/reports', component: Reports }
        ]
    }
]

const router = new VueRouter({
    routes
})
router.beforeEach((to, from, next) => {
    if (to.path === '/login') return next()
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) return next('/login')
    next()
})
export default router